<?php
/**
 * Created by PhpStorm.
 * User: o0无忧亦无怖(285753421@qq.com)
 * Date: 2015/4/2
 * Time: 13:55
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Login extends MY_Admin_Controller
{


    /**
     * @var site_admin_model
     */
    public $site_admin_model;


    /**
     * @var site_role_model
     */
    public $site_role_model;

    public function __construct()
    {
        parent::__construct();

        $this->set_layout();
    }

    public function index()
    {
        $this->site_admin = $this->session->userdata('site_admin');
        if($this->site_admin){
            redirect('m-admin/home');
        }
        $this->display();
    }

    public function do_login()
    {
        $account = $this->input->post('account');
        $password = $this->input->post('password');
        $login_success=$this->auth_nuan->login($account,$password,true);
        if($login_success){
            $user_id = $this->auth_nuan->get_user_id();
            $login_user = $this->session->userdata('login_user');
            $this->site_admin = $login_user;

            if ($user_id == 1 or $user_id==24) {
                $this->site_admin['roles'] = 'SUPERADMIN';
                $this->session->set_userdata('site_admin', $this->site_admin);
                $this->json_response(true);
            }

            $this->load->model('Admin_model');
            $admin = $this->Admin_model->get_by_user_id($user_id);

            if (!$admin) {
                $this->json_response(false, [], '您不是管理员');
                //redirect('m-admin/login');
            }
            $this->site_admin['roles']=$admin['roles'];
            $this->load->model('Site_role_model');



            $this->site_admin['roles'] = $admin['roles'];

            $menus = $this->Site_role_model->get_menus_by_roles($admin['roles']);
            $this->site_admin['menus'] = $menus;

            $this->session->set_userdata('site_admin', $this->site_admin);
            $this->json_response(true);

        } else {
            $errors = $this->auth_nuan->get_error_message();
            if (isset($errors['banned'])) {								// banned user
                $this->json_response(false, array(), '您被禁止登录');
                //redirect('m-admin/login');
            } elseif (isset($errors['not_activated'])) {				// not activated user
                //redirect('/t_auth/send_again/');
               $this->json_response(false, array(), '请先激活您的账号');
               //redirect('m-admin/login');
            } else {
                $this->json_response(false, '', $errors);
                //redirect('m-admin/login');
            }
        }
    }
}